package com.hospital.KT_Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.*;
import android.widget.*;
import com.hospital.BaseActivity.IndexBaseActivity;
import com.hospital.activity.R;
import com.hospital.tools.MyApplication;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by wangjun on 2015/7/2.
 */
public class MenuManagerActivity  extends IndexBaseActivity {
    TabHost tabHost;

    private LinearLayout toolbar;
    private FrameLayout bar_nav, bar_manage, bar_put, bar_message, bar_setting;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.matser);
       // MyApplication.getInstance().addActivity(this);
        toolbar = (LinearLayout) findViewById(R.id.toolbar);
        initButton();
        initTab();
      //  sub(Url.log_login);

    }





    public void initTab() {
        tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec("job").setIndicator("job")
                .setContent(new Intent(this, IndexActivity.class)));
//        tabHost.addTab(tabHost.newTabSpec("usercenter").setIndicator("usercenter")
//                .setContent(new Intent(this, CompanyCenterActivity.class)));
//        tabHost.addTab(tabHost.newTabSpec("put").setIndicator("put")
//                .setContent(new Intent(this, PostChose.class)));
//        tabHost.addTab(tabHost.newTabSpec("msg").setIndicator("msg")
//                .setContent(new Intent(this, MsgIndex2Activity.class)));
//        tabHost.addTab(tabHost.newTabSpec("setting").setIndicator("setting")
//                .setContent(new Intent(this, SettingIndexActivity.class)));
        tabHost.setCurrentTab(0);
    }


    public void initButton() {
        bar_nav = (FrameLayout) findViewById(R.id.toolbar_nav);
        bar_manage = (FrameLayout) findViewById(R.id.toolbar_manage);
        bar_put = (FrameLayout) findViewById(R.id.toolbar_put);
        bar_message = (FrameLayout) findViewById(R.id.toolbar_message);
        bar_setting = (FrameLayout) findViewById(R.id.toolbar_setting);

        View.OnTouchListener onTouch = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (view.getId()){
                    case R.id.toolbar_nav:
                        toolbar.setBackgroundResource(R.drawable.meun1);
                        tabHost.setCurrentTab(0);
                        break;
                    case R.id.toolbar_manage:
                        toolbar.setBackgroundResource(R.drawable.meun2);
                        tabHost.setCurrentTab(1);
                        break;
                    case R.id.toolbar_put:
                        toolbar.setBackgroundResource(R.drawable.meun3);
                        tabHost.setCurrentTab(2);
                        break;
                    case R.id.toolbar_message:
                        toolbar.setBackgroundResource(R.drawable.meun4);
                        tabHost.setCurrentTab(3);
                        break;
                    case R.id.toolbar_setting:
                        toolbar.setBackgroundResource(R.drawable.meun5);
                        tabHost.setCurrentTab(4);
                        break;
                    default:
                        break;
                }
                return true;
            }
        };

        bar_nav.setOnTouchListener(onTouch);
        bar_manage.setOnTouchListener(onTouch);
        bar_put.setOnTouchListener(onTouch);
        bar_message.setOnTouchListener(onTouch);
        bar_setting.setOnTouchListener(onTouch);
    }



    Toast backToast;

    boolean FINISH = false;

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN
                && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (!FINISH) {
                backToast = Toast
                        .makeText(this, "再按一次，退出程序", Toast.LENGTH_SHORT);
                backToast.show();
                FINISH = true;
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        FINISH = false;
                    }
                }, 2000);
                return true;
            } else {
              //  sub(Url.log_logout);

                return super.dispatchKeyEvent(event);
            }

        } else {
            return super.dispatchKeyEvent(event);
        }
    }
}
